#!/usr/bin/python2.5

# Copyright 2009 Ferdjaoui Sahid.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import logging

from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError

from django.conf import settings
from django.template import RequestContext, loader
from django.http import HttpResponse, HttpResponseRedirect

from facebook import djangofb as facebook

from common import models
from common import api


@facebook.require_login ()
def canvas_canvas (request):
  sended_actors   = api.entry_get_top_sended ()
  received_actors = api.entry_get_top_received ()
  
  t = loader.get_template ('canvas/templates/canvas.html')
  c = RequestContext (request, locals ())
  return HttpResponse (t.render (c))


@facebook.require_login ()
def canvas_actor (request, uid):
  
  view_ref  = api.user_get_or_create (uid)
  
  entries_s = models.Entry.gql ("WHERE owner = :owner  LIMIT 10", owner = view_ref).fetch (10);
  entries_r = models.Entry.gql ("WHERE target = :target  LIMIT 10", target = view_ref).fetch (10);
    
  request.user = view_ref

  t = loader.get_template ('canvas/templates/actor.html')
  c = RequestContext (request, locals ())
  return HttpResponse (t.render (c))


@facebook.require_login ()
def canvas_new (request):
  area = 'slap'

  slapup = request.REQUEST.get ('slapup', None)
  if slapup:
    owner   = request.user.uid;
    target  = request.REQUEST.get ('target')
    comment = request.REQUEST.get ('comment')
    
    try:
      if not target or not comment:
        raise Exception ("All fields are required")
      
      owner_ref  = api.user_get_or_create (owner)
      target_ref = api.user_get_or_create (target)
      entry_ref  = models.Entry (owner   = request.user, 
                                 target  = target_ref, 
                                 comment = comment)
      
      owner_ref.count_sended    = owner_ref.count_sended + 1
      target_ref.count_received = target_ref.count_received + 1
      
      entry_ref.put ()
      owner_ref.put ()
      target_ref.put ()
      
      request.user = owner_ref
      
      request.facebook.notifications.send (target, 'gives you a slap !, you want know why ?', 'user_to_user');
      request.facebook.feed.publishUserAction ('102578226618', {'comment' : comment}, target, 2)
      
      send_ok = True;
    except CapabilityDisabledError:
      error_msg = "For unplanned maintenance, this feature is desable."
    except Exception, e:
      error_msg = str (e)
      
  t = loader.get_template ('canvas/templates/new.html')
  c = RequestContext (request, locals ())
  return HttpResponse (t.render (c))


@facebook.require_login ()
def canvas_given (request):
  area = 'slap/given'

  entries = models.Entry.gql ("WHERE owner = :owner LIMIT 10", owner = request.user).fetch (10);
  
  t = loader.get_template ('canvas/templates/given.html')
  c = RequestContext (request, locals ())
  return HttpResponse (t.render (c))


@facebook.require_login ()
def canvas_received (request):
  area = 'slap/received'

  entries = models.Entry.gql ("WHERE target = :target  LIMIT 10", target = request.user).fetch (10);
  
  t = loader.get_template ('canvas/templates/received.html')
  c = RequestContext (request, locals ())
  return HttpResponse (t.render (c))



